import java.text.MessageFormat;

import choco.Choco;
import choco.cp.model.CPModel;
import choco.cp.solver.CPSolver;
import choco.kernel.model.constraints.Constraint;
import choco.kernel.model.variables.integer.IntegerVariable;

public class Square implements Runnable {
	public static void main(String[] args) {
		new Square().run();
	}

	public void run() {
		int n = 5;
		int sum = n * (n * n + 1) / 2;
		CPModel md = new CPModel();
		IntegerVariable[][] var = new IntegerVariable[n][n];
		IntegerVariable[][] varCol = new IntegerVariable[n][n];
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) {
				var[i][j] = Choco.makeIntVar("var_" + i + " " + j, 1, n * n);
				md.addVariable(var[i][j]);
				varCol[j][i] = var[i][j];
			}
		}
		for (int i = 0; i < n * n; i++) {
			for (int j = i + 1; j < n * n; j++) {
				Constraint c = ((Choco
						.neq(var[i / n][i % n], var[j / n][j % n])));
				md.addConstraint(c);
			}
		}
		for (int i = 0; i < n; i++) {
			md.addConstraint(Choco.eq(Choco.sum(var[i]), sum));
			md.addConstraint(Choco.eq(Choco.sum(varCol[i]), sum));
		}
		IntegerVariable[] diag1 = new IntegerVariable[n];
		// IntegerVariable[] diag2 = new IntegerVariable[n];
		for (int i = 0; i < n; i++) {
			diag1[i] = var[i][i];
			// diag2[i] = var[n - i - 1][i];
		}
		// md.addConstraint(Choco.eq(Choco.sum(diag1), sum));
		// md.addConstraint(Choco.eq(Choco.sum(diag2), sum));
		// md.addConstraint(Choco.max(var[0], ));

		int low = 0;
		int high = 1000;
		for (int i = 0; i < 20; i++) {
			int mid = (low + high - 1) / 2;
			Constraint c = Choco.gt(Choco.sum(diag1), mid);
			md.addConstraint(c);
			CPSolver sl = new CPSolver();
			sl.read(md);
			md.remove(c);
			sl.solve();
			if (sl.existsSolution()) {
				low = mid;
			} else {
				high = mid;
			}

		}
		// System.out.println(low + " " + high);
		Constraint c = Choco.gt(Choco.sum(diag1), low);
		md.addConstraint(c);
		CPSolver sl = new CPSolver();
		sl.read(md);
		sl.solve();

		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) {
				System.out.print(sl.getVar(var[i][j]).getVal() + " ");
			}
			System.out.println();
		}
	}
}
